package indi.kider.server.woniubus.service;

import indi.kider.common.model.apartment.Houses;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.employee.Tenant;
import indi.kider.common.model.woniubus.TenantInHouse;
import indi.kider.common.model.woniubus.TenantInHouseVo;
import indi.kider.common.model.woniubus.TenantVo;
import indi.kider.server.apartment.mapper.HousesMapper;
import indi.kider.server.employee.mapper.EmployeeMapper;
import indi.kider.server.employee.mapper.TenantInHouseMapper;
import indi.kider.server.woniubus.service.related.BillService;
import indi.kider.server.woniubus.service.related.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;
import pers.vankid.common.utils.page.PageUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("ALL")
public class PartServiceImpl implements PartService {
    @Autowired
    private TenantInHouseMapper tenantInHouseMapper;
    @Autowired
    private WoniuBusService woniuBusService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private HousesMapper housesMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private BillService billService;

    @Override
    public PageResponse<List<Map>> getTenantInHouseList(PageRequest<TenantInHouseVo> request) {
        PageUtil.start(request);
        return PageUtil.getPageResponse(tenantInHouseMapper.getTenantInHouseList(request.getQuery()));
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public void tenantCheckIn(TenantInHouse tenantInHouse, TenantVo tenantVo) throws Exception{
        Houses house = housesMapper.selectByPrimaryKey(tenantInHouse.getHousesId());
        if(tenantInHouse.getState() == 2){//同意入住
            tenantVo.setHouseId(tenantInHouse.getHousesId());
            CommonResponse c =  woniuBusService.rentHouse(tenantVo);
            //调用 账单接口=======创建钱包==================
            try{
                if(c.getData()!=null){
                    Tenant t = (Tenant)c.getData();
                    billService.regitWallet(t.getId());
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            //发送短信
            TenantInHouse sms = new TenantInHouse();
            sms.setPhone(tenantVo.getPhone());
            sms.setHouseNum(house.getHouseNumb());
            smsService.tenantSuccessSms(sms);
        }else{//拒绝入住
            List<Employee> list = employeeMapper.getEmployeePartByHouseId(house.getId());
            if(list!=null&&list.size()>0){
                Employee employee = list.get(0);
                TenantInHouse sms = new TenantInHouse();
                sms.setPhone(tenantVo.getPhone());
                sms.setPartName(employee.getName());
                sms.setPartPhone(employee.getPhone());
                smsService.tenantFailSms(sms);
            }
        }
        tenantInHouse.setTenantName(tenantVo.getName());
        tenantInHouse.setOpTime1(new Date());
        tenantInHouseMapper.updateByPrimaryKeySelective(tenantInHouse);
    }
}
